home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_lib.arc / PCENVCHK.C < prev    next >
Text File  |  1990-08-09  |  5KB  |  151 lines

  1. /**
  2. *
  3. *  Name         pcenvchk -- Return an environment parameter specification
  4. *
  5. *  Synopsis     pparm = pcenvchk(pspec,len);
  6. *               char *pparm       The returned parameter
  7. *               char *pspec       The environment specification for which
  8. *                                 to search
  9. *               int  len          The maximum number of characters of the
  10. *                                 parameter to return.
  11. *
  12. *  Description  This function searches the environment for the parameter
  13. *               associated with the specfication pointed to by pspec.  The
  14. *               parameter is returned in the string pointed to pparm, but
  15. *               no more than len characters are returned.  The string pparm
  16. *               is allocated space by PCENVCHK, and the space should be
  17. *               freed by the calling function when no longer needed.
  18. *               Only the first 127 characters of the environment are
  19. *               searched. The environment specification must be specified
  20. *               in upper case.
  21. *
  22. *  Returns      pparm             Pointer to the parameter string
  23. *
  24. *  Version      1.1  (C)Copyright Blaise Computing Inc.  1983, 1984
  25. *
  26. **/
  27. #include <compiler.h>
  28.  
  29. #if LDATA
  30. #define NULL    0L
  31. #else
  32. #define NULL    0
  33. #endif
  34.  
  35. struct segads                          /* Offset, segment address type */
  36. {
  37.   unsigned r;
  38.   unsigned s;
  39. };
  40. #define ADS     struct segads          /* Abbreviation                 */
  41.  
  42. #if LAT200
  43. extern ADS _psp;                       /* Program segment prefix       */
  44. #endif
  45. #if CI201A
  46. extern ADS _pspseg;
  47. #endif
  48. #if LAT104 | CI133D
  49. extern unsigned _pgmseg;
  50. #endif
  51.  
  52. char *pcenvchk(pspec,len)
  53. char *pspec;
  54. int  len;
  55. {
  56.  
  57.     ADS      env_ads,env_loc;
  58.     unsigned envseg,cs,ss,ds,es;
  59.     char     *penv_str,*pparm,*ptemp,*calloc();
  60.     int      len_env,len_spec,len_search,i,j,utslmove();
  61. #if CI201A & LDATA
  62.     unsigned long ptrtoabs();
  63. #endif
  64.  
  65.     /*  First move the first 128 bytes of the environment to a string. */
  66.  
  67. #if LAT104 | CI133D
  68.     env_loc.s = _pgmseg;               /* The segment address of the   */
  69. #endif                                 /* environment is at offset 2C  */
  70. #if LAT200                             /* in the program segment prefix*/
  71.     env_loc.s = _psp.s;
  72. #endif
  73. #if CI201A
  74.     env_loc.s = _pspseg.s;
  75. #endif
  76.     env_loc.r = 0x2c;
  77. #if LDATA
  78. #if CI201A
  79.     env_ads.s = (unsigned)((ptrtoabs(&envseg) & 0xffff0L) >> 4L);
  80.     env_ads.r = (unsigned)(ptrtoabs(&envseg) & 0xfL);
  81. #else
  82.     env_ads.s = (unsigned)(((long)(&envseg) & 0xffff0L) >> 4L);
  83.     env_ads.r = (unsigned)((long)(&envseg) & 0xfL);
  84. #endif
  85. #else
  86.     utsreg(&cs,&ss,&ds,&es);           /* Return segment reg values    */
  87.     env_ads.s = ds;
  88.     env_ads.r = &envseg;
  89. #endif
  90.     utslmove(&env_loc,&env_ads,2);     /* envseg now has segment       */
  91.  
  92.     penv_str  = calloc(128,1);         /* Put environment string here  */
  93.     ptemp     = penv_str;              /* Save pointer to storage      */
  94.     env_loc.s = envseg;
  95.     env_loc.r = 0;
  96. #if LDATA
  97. #if CI201A
  98.     env_ads.s = (unsigned)((ptrtoabs(penv_str) & 0xffff0L) >> 4L);
  99.     env_ads.r = (unsigned)(ptrtoabs(penv_str) & 0xfL);
  100. #else
  101.     env_ads.s = (unsigned)(((long)(penv_str) & 0xffff0L) >> 4L);
  102.     env_ads.r = (unsigned)((long)(penv_str) & 0xfL);
  103. #endif
  104. #else
  105.     env_ads.s = ds;
  106.     env_ads.r = penv_str;
  107. #endif
  108.     utslmove(&env_loc,&env_ads,128);
  109.  
  110.     /* Now determine the length of the environment.  The environment   */
  111.     /* is terminated by two null bytes, so search for these.           */
  112.  
  113.     len_env = 0;
  114.     for (j = 0,len_env = 0; (j < 128) && (len_env == 0); j++)
  115.     {
  116.         if (*penv_str++ == 0)
  117.            if (*penv_str++ == 0)
  118.               len_env = j + 1;
  119.            else
  120.               j++;
  121.     }
  122.     penv_str -= len_env + 1;           /* Point to beginning of string */
  123.  
  124.     /* Now search the environment string for the specification string  */
  125.  
  126.     len_spec   = strlen(pspec);
  127.     len_search = len_env - len_spec + 1;
  128.     for (i = 0; i < len_search; i += j + 1)
  129.     {
  130.         for (j = 0; (j < len_spec) && (*pspec++ == *penv_str++); j++);
  131.         if (j == len_spec)
  132.            break;                      /* penv_str points to parameter */
  133.         pspec -= j + 1;                /* search again                 */
  134.     }
  135.     if (i >= len_search)
  136.     {
  137.        free(ptemp);
  138.        return(NULL);                   /* Not found in the environment */
  139.     }
  140.  
  141.     /* Copy the parameter string to pparm.                             */
  142.  
  143.     pparm = calloc(len + 1,1);
  144.     for (i = 0; (i < len) && (*pparm++ = *penv_str++); i++);
  145.     pparm -= i + 1;
  146.     free(ptemp);                       /* Release storage for penv_str */
  147.  
  148.     return(pparm);
  149.  
  150. }
  151.